﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;

namespace LINQPractical.CLASSES
{
    public static class StringExt
    {
        public static int ToInt(this string str)
        {
            if (string.IsNullOrEmpty(str))
                return 0;
            int num;
            var result = Int32.TryParse(str, System.Globalization.NumberStyles.Number, System.Threading.Thread.CurrentThread.CurrentCulture.NumberFormat, out num);
            return result ? num : int.MinValue;
        }
        public static Guid? ToGuid(this string str)
        {
            if (string.IsNullOrEmpty(str))
                return null;
            const string guid = "[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}";
            if (System.Text.RegularExpressions.Regex.IsMatch(str, guid))
                return new Guid(str);
            return null;
        }
        public static double ToDouble(this string number)
        {
            double num;
            var result = double.TryParse(number, out num);
            return result ? num : 0;
        }
        public static bool IsInt(this string str)
        {
            if (string.IsNullOrEmpty(str))
                return false;
            int num;
            return Int32.TryParse(str, System.Globalization.NumberStyles.Number, System.Threading.Thread.CurrentThread.CurrentCulture.NumberFormat, out num);
        }
        public static bool IsDate(this string str)
        {
            if (string.IsNullOrEmpty(str))
                return false;
            DateTime num;
            return DateTime.TryParse(str, out num);
        }
        public static DateTime? ToUSDateTimeUTC(this string str)
        {
            var provider = System.Globalization.CultureInfo.InvariantCulture;
            try
            {
                return DateTime.ParseExact(str, "dd/MM/yyyy h:mm:ss tt zzz", provider);
            }
            catch
            {
            }
            return null;
        }
        public static string Right(this string str, int length)
        {
            return str.Substring(str.Length - length, length);
        }
        public static string Left(this string str, int length)
        {
            return str.Substring(0, length);
        }
        public static bool Contains(this string[] stringArray, string text)
        {
            if (stringArray == null || stringArray.Length == 0 || string.IsNullOrEmpty(text))
                return false;
            return stringArray.Any(thisString => thisString.Equals(text, StringComparison.CurrentCultureIgnoreCase));
        }
        public static string ToCommaDelimited(this string[] arr)
        {
            if (arr == null || arr.Length == 0)
                return string.Empty;
            var sb = new StringBuilder();
            foreach (string t in arr)
            {
                sb.Append(t + ",");
            }
            return sb.RemoveLastChars(1).ToString();
        }
        public static string ToUpperFirstLetter(this string text)
        {
            if (string.IsNullOrEmpty(text.Trim()))
                return string.Empty;
            return text.Substring(0, 1).ToUpper() + text.Substring(1, text.Length - 1);
        }
        //public static string ToPluralWithParenthesis(this string Text, bool firstLetterToUpper)
        //{
        //    const string s = "(s)";
        //    if (!Text.EndsWith(s, StringComparison.InvariantCultureIgnoreCase))
        //        Text += s;
        //    if (firstLetterToUpper)
        //        Text = SubSonic.Sugar.Strings.CamelToProper(Text);
        //    return Text;
        //}
        //public enum UperizeOption { FirstLetter, FirstLetterOfWords }
        //public static string Upperize(string text, UperizeOption option)
        //{
        //    string result = string.Empty;
        //    switch (option)
        //    {
        //        case UperizeOption.FirstLetter:
        //            result = UperizeFirstLetter(text);
        //            break;
        //        case UperizeOption.FirstLetterOfWords:
        //            result = UpperizeWords(text);
        //            break;
        //    }
        //    return result.Trim();
        //}
        //private static string UperizeFirstLetter(string text)
        //{
        //    string result = text.Substring(0, 1).ToUpper() + text.Remove(0, 1);
        //    return result;
        //}
        //public static string ToPlural(this string text, bool firstLetterToUpper)
        //{
        //    const string s = "s";
        //    if (!text.EndsWith(s, StringComparison.InvariantCultureIgnoreCase))
        //        text += s;
        //    if (firstLetterToUpper)
        //        text = SubSonic.Sugar.Strings.CamelToProper(text);
        //    return text;
        //}
        /// <summary>
        /// change quotes to ´ character and / to \/
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string SmartSingleQuoted(this string str)
        {
            return string.IsNullOrEmpty(str)
                ? string.Empty
                : str.Replace("'", "\\'");
        }

        /// <summary>
        /// HTML Encode single quotes
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string HtmlSingleQuoted(this string str)
        {
            return string.IsNullOrEmpty(str)
                ? string.Empty
                : str.Replace("'", "&quot;");
        }

        //public static void ToPlural(this Label label, bool firstLetterToUpper)
        //{
        //    string s = "s";
        //    if (!label.Text.EndsWith(s, StringComparison.InvariantCultureIgnoreCase))
        //        label.Text += s;
        //    if (firstLetterToUpper)
        //        label.Text = SubSonic.Sugar.Strings.CamelToProper(label.Text);
        //}
        //public static void ToPluralWithParenthesis(this Label label, bool firstLetterToUpper)
        //{
        //    string s = "(s)";
        //    if (!label.Text.EndsWith(s, StringComparison.InvariantCultureIgnoreCase))
        //        label.Text += s;
        //    if (firstLetterToUpper)
        //        label.Text = SubSonic.Sugar.Strings.CamelToProper(label.Text);
        //}
        public static string Delimited(this List<string> list, string delimiter)
        {
            var sb = new StringBuilder();
            foreach (var item in list)
                sb.Append(item + delimiter);
            return sb.ToString().TrimEnd(delimiter.ToCharArray());
        }

    }
}
